﻿Imports System.Data.OleDb


Public Class AddHotel
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

    Protected Sub btn_AddNewHotel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_AddNewHotel.Click

        Dim newFileName As String

        newFileName = Guid.NewGuid().ToString() & getSuffix(f_Picture.FileName)

        If f_Picture.HasFile Then
            f_Picture.SaveAs(Server.MapPath("Image") & "/" & newFileName)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HatConnStr").ConnectionString)
        Dim SqlString As String = "Insert into HotelMainTable(HotelName,Location,Stars,PostCode,ContactDetails,HotelUrl,Description,Picture) Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_HotelName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_Location.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Stars.Text)
        cmd.Parameters.AddWithValue("@f4", tb_PostCode.Text)
        cmd.Parameters.AddWithValue("@f5", tb_ContactDetails.Text)
        cmd.Parameters.AddWithValue("@f6", tb_HotelUrl.Text)
        cmd.Parameters.AddWithValue("@f7", tb_Description.Text)
        cmd.Parameters.AddWithValue("@f8", newFileName)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        Response.Redirect("~/list.aspx")
    End Sub
End Class